<template>
  <div class="contentDiv">
    <div class="jgdiv"></div>
    <div class="ctdiv">
      <ul>
        <li v-for="(device, index) in devices" :key="index">
          <div class="mkdiv"  @click="gopage('/pages/device/main', device.id, device.projectid, device.deviceid)">
            <div class="divss1">
              <img src="http://qiniuyun.bblog.site/dun.png" class="imgcls"/>
            </div>
            <div class="divss2">
              <span class="contentcls1">项目号- {{ device.projectid }}</span>
              <span class="contentcls2">设备号- {{ device.deviceid }}</span>
            </div>
            <div class="divss3">
              <img src="http://qiniuyun.bblog.site/xiayiye.png" class="tocls"/></div>
            </div>
        </li>
      </ul>
    </div>
    <div class="sbdiv">
      <span>共有 {{ dsum }} 台设备</span>
    </div>

    <div class="saoyisao">
      <button class="butsss" @click="saoyisao()">扫一扫</button>
    </div>
  </div>
</template>
<script>
import { requestPost } from '@/utils/index'
export default {
  data () {
    return {
      devices: [],
      dsum: '0',
      openid: '',
      dd: [],
      pr: [],
      de: []
    }
  },
  onLoad () {
    wx.setNavigationBarTitle({
      title: '智能门锁'
    })
    this.openid = wx.getStorageSync('openid')
    this.xcxregister()
  },
  onShow () {
    this.getDevices()
  },
  methods: {
    saoyisao () {
      wx.scanCode({
        success: (res) => {
          this.dd = res.result.split('&')
          this.pr = this.dd[0].split('=')
          this.de = this.dd[1].split('=')
          var that = this
          var data = {
            'obj': {'openid': this.openid, 'projectid': this.pr[1], 'deviceid': this.de[1]}
          }
          var url = wx.getStorageSync('xcxrequestUrl') + '/xcx/harduser/bindDevice'
          wx.showLoading({
            title: '设备正在绑定..'
          })
          requestPost(url, data, 'POST').then(function (value) {
            wx.showLoading({
              title: '设备绑定成功'
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 1500)
            that.getDevices()
          }, function (value) {
            setTimeout(function () {
              wx.hideLoading()
            }, 1500)
          })
        }
      })
    },
    xcxregister () {
      var data = {
        'obj': {'openid': this.openid}
      }
      var url = wx.getStorageSync('xcxrequestUrl') + '/xcx/harduser/xcxregister'
      requestPost(url, data, 'POST').then(function (value) {}, function (value) {})
    },
    gopage (pathstr, id, projectid, deviceid) {
      wx.navigateTo({
        url: pathstr + '?id=' + id + '&projectid=' + projectid + '&deviceid=' + deviceid
      })
    },
    getDevices () {
      this.devices = []
      this.dsum = 0
      var that = this
      var data = {
        'obj': {'openid': this.openid}
      }
      var url = wx.getStorageSync('xcxrequestUrl') + '/xcx/harduser/queryMyBindDevices'
      requestPost(url, data, 'POST').then(function (value) {
        for (var ele of value.data) {
          that.devices.push(ele)
        }
        that.dsum = value.data.length
      }, function (value) {
      })
    }
  }
}
</script>
<style scoped>
html, body{
  margin: 0;
  padding: 0;
  background-color: rgba(216, 216, 216, 0.144);
}
.saoyisao{
  bottom: 0;
  position: fixed;
  width: 100%;
  text-align: center;
}
.butsss{
  width: 70%;
  margin: 0 auto 10px;
  border:0px solid red;
}
.sbdiv{
  text-align: center;
  color: rgb(211, 211, 211);
  font-size: 15px;
  margin-top: 10px;
}
.jgdiv{
  /* margin-top: 50px; */
  height: 10px;;
}
.contentDiv{
  position: fixed;
  height: 100%;
  width: 100%;
  display:flex;
  flex-direction:column;
  /* align-items:center;/*垂直居中*/
  /* justify-content: center;水平居中  */
  background-color: rgba(228, 228, 228, 0.171);
}
.mkdiv{
  background-color: rgb(255, 255, 255);
  height: 60px;
  line-height: 60px;
}
.divss1{
  float: left;
  width: 15%;
  height: 60px;
  margin-left: 5px;
  text-align: center;
}
.divss2{
  float: left;
  width: 60%;
  height: 60px;
  line-height: 6px;
}
.divss3{
  float: right;
  width: 10%;
  height: 60px;
}
.contentcls1{
  font-size: 18px;
  width: 80%;
  height: 40px;
  float: left;
  line-height: 50px;
}
.contentcls2{
  color: rgba(173, 173, 173, 0.959);
  font-size: 12px;
  width: 80%;
  height: 20px;
  float: left;
}
.imgcls{
  width: 35px;
  height: 35px;
  vertical-align: middle;
}
.tocls{
  width: 20px;
  height: 20px;
  vertical-align: middle;
}

</style>
